package com.gs.m_home.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gs.common.API
import com.gs.common.constant.ActionConst
import com.gs.common.global.GlobalVariables
import com.gs.common.util.ToastUtil
import com.gs.m_home.R
import com.gs.m_home.adapter.GoodsListAdapter
import com.gs.m_home.callback.OnDoGetGoodsStateCallback
import com.gs.common.data.Good
import com.gs.common.listener.DataListener
import com.gs.m_home.callback.OnDoGetGoodsByQueryStateCallback
import com.gs.m_home.persistent.ShoppingPersistent

class ShoppingFragment : Fragment(), DataListener {

    private val shoppingPersistent by lazy { ShoppingPersistent.getInstance(requireContext()) }
    private val mGoodData = ArrayList<Good>()
    private lateinit var mLoadingTv: TextView
    private lateinit var mGoodsListRecyclerView: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        GlobalVariables.dataListener = this
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.shopping_fragment, container, false)

        mLoadingTv = view.findViewById(R.id.loading_tv)
        mGoodsListRecyclerView = view.findViewById<RecyclerView>(R.id.goods_list)

        mLoadingTv.visibility = View.VISIBLE
        mGoodsListRecyclerView.visibility = View.GONE

        // 配置 goods list recyclerView
        val layoutManager = GridLayoutManager(requireContext(), 2)
        mGoodsListRecyclerView.layoutManager = layoutManager
        val adapter = GoodsListAdapter(requireContext(), mGoodData)
        mGoodsListRecyclerView.adapter = adapter
        // 为 goods list item 添加点击事件
        adapter.setGoodsListItemClickListener(object : GoodsListAdapter.OnGoodsListItemClickListener {
            override fun onItemClick(position: Int) {
                val intent = Intent().apply {
                    action = ActionConst.TO_GOODS_DETAIL_ACTIVITY_ACTION
                    removeExtra("position")
                    putExtra("position", position)
                }
                startActivity(intent)
//                parentFragmentManager.beginTransaction()
//                    .replace(R.id.nav_host_fragment_activity_home, GoodsDetailFragment())
//                    .addToBackStack(null)
//                    .commit()
            }
        })


        return view
    }

    override fun onStart() {
        super.onStart()
        mLoadingTv.visibility = View.GONE
        showGoods()
        mGoodsListRecyclerView.visibility = View.VISIBLE
    }

    override fun onResume() {
        super.onResume()
    }

    private fun showGoods() {
        shoppingPersistent.getGoods(object : OnDoGetGoodsStateCallback{
            override fun onGetGoodsSuccess() {
                val goodsList = GlobalVariables.goodsList
                if (goodsList == null) {
                    ToastUtil.showShortToast(requireContext(), "获取商品失败！")
                    return
                }
                mGoodData.clear()
                for (good in goodsList) {
                    good.picture_path = "http:/${API.IP}:${API.PORT}${good.picture_path}"
                    if (good.count > 0) {
                        mGoodData.add(good)
                    }
                }
                mGoodsListRecyclerView.adapter?.notifyDataSetChanged()
            }

            override fun onGetGoodsFailed() {
                ToastUtil.showShortToast(requireContext(), "获取商品失败！")
            }
            override fun onNetworkError() {
                ToastUtil.showShortToast(requireContext(), "无法连接网络！")
            }

            override fun onServerError() {
                ToastUtil.showShortToast(requireContext(), "无法连接服务器！")
            }
        })
    }

    override fun onDataReceived(data: String) {
        shoppingPersistent.getGoodsByQuery(data, object : OnDoGetGoodsByQueryStateCallback{
            override fun onGetGoodsByQuerySuccess() {
                val goodsList = GlobalVariables.goodsList
                if (goodsList == null) {
                    ToastUtil.showShortToast(requireContext(), "获取商品失败！")
                    return
                }
                mGoodData.clear()
                for (good in goodsList) {
                    good.picture_path = "http:/${API.IP}:${API.PORT}${good.picture_path}"
                    mGoodData.add(good)
                }
                mGoodsListRecyclerView.adapter?.notifyDataSetChanged()
            }

            override fun onGetGoodsByQueryFailed() {
                ToastUtil.showShortToast(requireContext(), "搜索商品失败！")
            }

            override fun onNetworkError() {
                ToastUtil.showShortToast(requireContext(), "无法连接网络！")
            }

            override fun onServerError() {
                ToastUtil.showShortToast(requireContext(), "无法连接服务器！")
            }
        })
    }
}